package gui;



import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import entities.Device;
import entities.DeviceType;

public class DeviceList {
	public WindowsApplication windowsApp;
	private JPanel pnlDeviceList;
	private JList<String> lstConnectedDevices;	
	
	public DeviceList(){
		initialize();
	}
	public DeviceList(WindowsApplication windowsApp){
		this.windowsApp = windowsApp;
		initialize();
	}
	private void initialize(){
		pnlDeviceList = new JPanel();
		pnlDeviceList.setBounds(0, 0, 800, 600);
		pnlDeviceList.setLayout(null);
		
		JLabel lblPage = new JLabel("Connected Device Listing");
		lblPage.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 25));
		lblPage.setBounds(0, 13, 800, 30);
		lblPage.setHorizontalAlignment(SwingConstants.CENTER);
		pnlDeviceList.add(lblPage);
		
		JLabel lblDeviceName = new JLabel("Device Name: " + windowsApp.getDevice().getDeviceName());
		lblDeviceName.setBounds(50, 50, 200, 22);
		pnlDeviceList.add(lblDeviceName);
		
		JLabel lblDeviceIp = new JLabel("Device IP: " + windowsApp.getDevice().getIpAddress());
		lblDeviceIp.setBounds(50, 75, 200, 22);
		pnlDeviceList.add(lblDeviceIp);
		
		JLabel lblDeviceUUID = new JLabel("Device UUID: " + windowsApp.getDevice().getUUID());
		lblDeviceUUID.setBounds(50, 100, 200, 22);
		pnlDeviceList.add(lblDeviceUUID);
		
		DefaultListModel<String> listModel = new DefaultListModel<String>();
		for(String s : windowsApp.getDevice().getConnectedDevices()){
			listModel.addElement(s);
		}
		//The list should display the Devices Name and IP
		//Each deviceName and IP should have one value that is the Mac Address
		lstConnectedDevices = new JList<String>();
		lstConnectedDevices.setBounds(12, 187, 600, 400);
		lstConnectedDevices.setModel(listModel);
		pnlDeviceList.add(lstConnectedDevices);
		
		JButton btnViewDevice = new JButton("View Device");
		btnViewDevice.setBounds(668, 187, 120, 50);
		btnViewDevice.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Device target = new Device();
				
				target = windowsApp.getCSEP().getConnectionServiceImpPort().getDeviceByUUID(lstConnectedDevices.getSelectedValue());
				windowsApp.getFrame().setContentPane(new DeviceOverview(windowsApp, target).getPanel());
			}
		});
		pnlDeviceList.add(btnViewDevice);
		
		JButton btnBack = new JButton("Back");
		btnBack.setBounds(668, 562, 120, 25);
		btnBack.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				windowsApp.getFrame().setContentPane(new Overview(windowsApp).getPanel());
			}
		});
		pnlDeviceList.add(btnBack);
	}
	
	public JPanel getPanel(){
		return pnlDeviceList;
	}
}
